package com.example.apidocserver.domain.user.service.impl;

import com.example.apidocserver.domain.user.entity.UsrUserAuths;
import com.example.apidocserver.domain.user.entity.UsrUsers;
import com.example.apidocserver.domain.user.dao.UsrUsersDao;
import com.example.apidocserver.domain.user.service.UsrUsersService;
import com.example.apidocserver.infrastructure.config.security.UserDetailsImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * (UsrUsers)表服务实现类
 *
 * @author makejava
 * @since 2023-04-26 22:20:16
 */
@Service("usrUsersService")
@Slf4j
public class UsrUsersServiceImpl implements UsrUsersService {
    @Resource
    private UsrUsersDao usrUsersDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public UsrUsers queryById(Long userId) {
        return this.usrUsersDao.queryById(userId);
    }

    /**
     * 查询所有对象
     *
     * @return 查询结果
     */
    @Override
    public List<UsrUsers> queryAll() {
        return this.usrUsersDao.queryAll();
    }

    /**
     * 根据对象查询
     *
     * @param usrUsers 筛选条件
     * @return 查询结果
     */
    @Override
    public List<UsrUsers> queryByEntity(UsrUsers usrUsers) {
        return this.usrUsersDao.queryByEntity(usrUsers);
    }

    /**
     * 新增数据
     *
     * @param usrUsers 实例对象
     * @return 实例对象
     */
    @Override
    public UsrUsers insert(UsrUsers usrUsers) {
        this.usrUsersDao.insert(usrUsers);
        return usrUsers;
    }

    /**
     * 修改数据
     *
     * @param usrUsers 实例对象
     * @return 实例对象
     */
    @Override
    public UsrUsers update(UsrUsers usrUsers) {
        this.usrUsersDao.update(usrUsers);
        return this.queryById(usrUsers.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.usrUsersDao.deleteById(userId) > 0;
    }

    @Override
    public UserDetails queryUserDeTailsByUsernameAndType(String username, Integer identityType) {
        if (Objects.isNull(identityType)) {
            return null;
        }
        UsrUserAuths auth = this.usrUsersDao.findUserAuthsByUsernameAndType(username, identityType);
        if (Objects.isNull(auth)) {
            return null;
        }
        UsrUsers user = usrUsersDao.queryById(auth.getUserId());
        if (Objects.isNull(user)) {
            return null;
        }
        UserDetailsImpl result = getUserDetails(auth, user);
        result.setDataRoleValList(Collections.singletonList(127));
        return result;
    }

    @Override
    public UserDetailsImpl getUserDetails(UsrUserAuths auth, UsrUsers user) {
        UserDetailsImpl userDetails = new UserDetailsImpl();
        userDetails.setUserId(user.getUserId()).setExpired(user.getExpired()).setEnabled(user.getEnabled())
                .setLocked(user.getLocked()).setAvatarImgId(user.getAvatarImgId()).setNickName(user.getNickName())
                .setIdentifier(auth.getIdentifier()).setCredential(auth.getCredential())
                .setIdentifierType(auth.getIdentifierType());
        return userDetails;
    }
}
